package JavaSE.CODE.javaCollection;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * TreeSet集合的使用
 * Comparator接口实现定制比较
 * 实现了Comparator就不用实现Comparable
 */
public class ComparatorDemo {
    public static void main(String[] args) {
        TreeSet<Person> personTreeSet = new TreeSet<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                int n1 = o1.getAge()-o2.getAge();
                int n2 = o1.getName().compareTo(o2.getName());
                return n1==0?n2:n1;
            }
        });
        personTreeSet.add(new Person("xyz",5));
        personTreeSet.add(new Person("abc",10));
        personTreeSet.add(new Person("hij",12));
        personTreeSet.add(new Person("eij",12));
        System.out.println(personTreeSet);
        //直接打印集合，集合会调用集合内类型的toString方法
    }
}
